package com.have.blog.xo.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.base.constant.BaseSysConstant;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.model.api.CommonPage;
import com.have.blog.model.mapper.TSubjectItemMapper;
import com.have.blog.model.mapper.TSubjectMapper;
import com.have.blog.model.models.TSubject;
import com.have.blog.model.models.TSubjectExample;
import com.have.blog.model.models.TSubjectItemExample;
import com.have.blog.utils.MyStringUtils;
import com.have.blog.utils.ResultUtils;
import com.have.blog.xo.constant.MessageConstant;
import com.have.blog.xo.service.admin.AdminBlogSubjectService;
import com.have.blog.xo.vo.AdminBlogSubjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@SuppressWarnings("all")
public class AdminBlogSubjectServiceImpl implements AdminBlogSubjectService {
    @Autowired
    private TSubjectMapper subjectMapper;
    @Autowired
    private TSubjectItemMapper itemMapper;

    @Override
    public String getPageList(AdminBlogSubjectVo subjectVO) {
        PageHelper.startPage(subjectVO.getCurrentPage(), subjectVO.getPageSize());
        TSubjectExample example = new TSubjectExample();
        TSubjectExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        if (!StringUtils.isEmpty(subjectVO.getKeyword()) && !StringUtils.isEmpty(subjectVO.getKeyword().trim())) {
            criteria.andSubjectNameLike("%" + subjectVO.getKeyword() + "%");
        }
        example.setOrderByClause("sort desc");
        List<TSubject> list = subjectMapper.selectByExample(example);

        StringBuilder sb = new StringBuilder();
        list.forEach(item -> {
            if (!StringUtils.isEmpty(item.getFileUid())) {
                sb.append(item.getFileUid() + BaseSysConstant.FILE_SEGMENTATION);
            }
        });

        // TODO 图片相关

        return ResultUtils.successWithData(CommonPage.restPage(list));
    }

    @Override
    public String addSubject(AdminBlogSubjectVo subjectVO) {
        TSubjectExample example = new TSubjectExample();
        example.createCriteria()
                .andSubjectNameEqualTo(subjectVO.getSubjectName())
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        List<TSubject> subjectList = subjectMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(subjectList)) {
            return ResultUtils.errorWithMessage(MessageConstant.ENTITY_EXIST);
        }
        TSubject subject = new TSubject();
        subject.setUid(MyStringUtils.getUUID());
        subject.setSubjectName(subjectVO.getSubjectName());
        subject.setClickCount(0);
        subject.setCollectCount(0);
        subject.setSort(subjectVO.getSort());
        subject.setFileUid(subjectVO.getFileUid());
        subject.setSummary(subjectVO.getSummary());
        subject.setStatus(BlogStatus.ENABLE.getStatus());
        subject.setUpdateTime(new Date());
        subject.setCreateTime(new Date());
        subjectMapper.insert(subject);
        return ResultUtils.successWithData(MessageConstant.INSERT_SUCCESS);
    }

    @Override
    public String editSubject(AdminBlogSubjectVo subjectVO) {
        TSubject subject = subjectMapper.selectByPrimaryKey(subjectVO.getUid());
        if (subject == null) {
            return ResultUtils.successWithData(MessageConstant.ENTITY_NOT_EXIST);
        }
        if (!subject.getSubjectName().equals(subjectVO.getSubjectName())) {
            TSubjectExample example = new TSubjectExample();
            example.createCriteria()
                    .andSubjectNameEqualTo(subjectVO.getSubjectName())
                    .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
            List<TSubject> tSubjects = subjectMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(tSubjects)) {
                return ResultUtils.errorWithMessage(MessageConstant.ENTITY_EXIST);
            }
        }
        subject.setSubjectName(subjectVO.getSubjectName());
        subject.setSummary(subjectVO.getSummary());
        subject.setFileUid(subjectVO.getFileUid());
        subject.setClickCount(subjectVO.getClickCount());
        subject.setCollectCount(subjectVO.getCollectCount());
        subject.setSort(subjectVO.getSort());
        subject.setUpdateTime(new Date());
        subjectMapper.updateByPrimaryKey(subject);
        return ResultUtils.successWithData(MessageConstant.UPDATE_SUCCESS);
    }

    @Override
    public String deleteBatchSubject(List<AdminBlogSubjectVo> subjectVOList) {
        if (CollectionUtils.isEmpty(subjectVOList)) {
            return ResultUtils.errorWithMessage(MessageConstant.PARAM_INCORRECT);
        }
        List<String> uids = new ArrayList<>();
        subjectVOList.forEach(item -> {
            uids.add(item.getUid());
        });
        TSubjectItemExample example = new TSubjectItemExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andSubjectUidIn(uids);
        long count = itemMapper.countByExample(example);
        if (count > 0) {
            return ResultUtils.errorWithMessage(MessageConstant.SUBJECT_UNDER_THIS_SORT);
        }

        TSubjectExample subjectExample = new TSubjectExample();
        subjectExample.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andUidIn(uids);
        TSubject subject = new TSubject();
        subject.setStatus(BlogStatus.DISABLE.getStatus());
        subjectMapper.updateByExampleSelective(subject, subjectExample);
        return ResultUtils.successWithData(MessageConstant.DELETE_SUCCESS);
    }
}
